package com.leather.job.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.leather.common.entity.Job;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.CheckKit;
import com.leather.common.toolkits.DateKit;
import com.leather.common.toolkits.ValidateKit;
import com.leather.job.mapper.JobMapper;
import com.leather.job.service.JobService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job> implements JobService {

    @Override
    public Result addJob(Job job) {
        Result result = validateJob(job);
        if (!result.isSuccess()) {
            return result;
        }
        CheckKit.check(save(job), CodeMsg.SERVER_ERROR);
        return Result.success("ok");
    }

    @Override
    public Result deleteJob(int jobId) {
        CheckKit.check(jobId > 0, CodeMsg.ERROR_JOB_ID);
        CheckKit.check(removeById(jobId), CodeMsg.ERROR_JOB_ID);
        return Result.success("ok");
    }

    @Override
    public Result jobIndex() {
        List<Job> jobs = query().gt("end_time", DateKit.now()).list();
        return Result.success(jobs);
    }

    @Override
    public IPage<Job> getAll(Page<Job> pages, String search) {
        IPage<Job> page = query().like(StringUtils.isNotBlank(search), "job_name", search).or().like(StringUtils.isNotBlank(search), "job_desc", search).page(pages);
        return page;
    }

    private Result validateJob(Job job) {
        if (!ValidateKit.isMobile(job.getMessage())) {
            return Result.error(CodeMsg.ERROR_PHONE);
        }

        if (StringUtils.isBlank(job.getJobName())) {
            return Result.error(CodeMsg.EMPTY_JOB_NAME);
        }
        if (StringUtils.isBlank(job.getJobDesc())) {
            return Result.error(CodeMsg.EMPTY_JOB_DESC);
        }
        if (StringUtils.isBlank(job.getAddress())) {
            return Result.error(CodeMsg.EMPTY_JOB_ADDRRSS);
        }
        if (DateKit.compare(DateKit.now(), job.getEndTime())) {
            return Result.error(CodeMsg.ERROR_JOB_END_TIME);
        }
        return Result.success(job);
    }

    @Override
    public Result getJobById(int jobId) {
        CheckKit.check(jobId>0,CodeMsg.ERROR_JOB_ID);
        Job job = getById(jobId);
        CheckKit.check(job!=null,CodeMsg.ERROR_JOB_ID);
        return Result.success(job);
    }

    @Override
    public Result searchJob(String search) {
        if(StringUtils.isBlank(search)) {
            return Result.success(Lists.newArrayList());
        }
        List<Job> list = query().like("job_name", search).or().like("job_desc", search).or().like("address", search).list();
        return Result.success(list);
    }
}
